package com.mf.all.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.all.exception.GraceException;
import io.micrometer.common.util.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import com.mf.all.entity.dto.DtoDic;
import com.mf.all.entity.req.search.basic.SearchReqDic;
import com.mf.all.mapper.MapperDic;
import com.mf.all.service.ServiceDic;
import org.springframework.transaction.annotation.Transactional;


@Service
public class ImplServiceDic extends ServiceImpl<MapperDic, DtoDic> implements ServiceDic {


    @Override
    public LambdaQueryWrapper<DtoDic> fillParams(SearchReqDic params) {
        final LambdaQueryWrapper<DtoDic> wrapper = loadQw();
        if (StringUtils.isNotBlank(params.getKey())) {
            wrapper.and(w -> {
                w.like(DtoDic::getDicLabel, params.getKey())
                        .or()
                        .like(DtoDic::getDicValue, params.getKey());
            });
        }
        wrapper.eq(ObjectUtils.isNotEmpty(params.getPid()), DtoDic::getPid, params.getPid())
                .eq(ObjectUtils.isNotEmpty(params.getStatus()), DtoDic::getStatus, params.getStatus());

        if(ObjectUtils.isNotEmpty(params.getDeptId())){
            wrapper.and(w -> {
                w.eq(DtoDic::getDeptId, params.getDeptId())
                        .or()
                        .eq(DtoDic::getDeptId, 0);
            });
        }
        return wrapper;
    }
    /**
     * 删除
     */
    @Override
    @Transactional
    public void deleteById(Integer id) {
        final LambdaQueryWrapper<DtoDic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtoDic::getPid, id);
        if(count(wrapper) > 0){
            GraceException.display("有子数据不能直接删除");
        }
        removeById(id);
    }
}